Option Explicit On
Option Strict On

Imports System.Collections
Imports System.Collections.Specialized

Public Class frmEAdetail
    Inherits System.Windows.Forms.Form
    Public Shared oEntity As New EAdetailClass
    Friend WithEvents combo_parentnode As System.Windows.Forms.ComboBox
    Dim parentNodes As Collection = New Collection()
    Friend WithEvents button_example As System.Windows.Forms.Button
    Public Shared bUpdate As Boolean = False

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        oEntity = New EAdetailClass
    End Sub

    Public Sub New(ByVal inEntity As EAdetailClass)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        oEntity = New EAdetailClass
        oEntity = inEntity
    End Sub
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtLabel As System.Windows.Forms.TextBox
    Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
    Friend WithEvents txtDef As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents txtDefSource As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmEAdetail))
        Me.Label1 = New System.Windows.Forms.Label
        Me.txtLabel = New System.Windows.Forms.TextBox
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.txtDef = New System.Windows.Forms.TextBox
        Me.txtDefSource = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.btnOK = New System.Windows.Forms.Button
        Me.btnCancel = New System.Windows.Forms.Button
        Me.combo_parentnode = New System.Windows.Forms.ComboBox
        Me.button_example = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(12, 9)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 23)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Entity Type Label"
        Me.ToolTip1.SetToolTip(Me.Label1, "the name of the entity type. ")
        '
        'txtLabel
        '
        Me.txtLabel.Location = New System.Drawing.Point(185, 6)
        Me.txtLabel.Name = "txtLabel"
        Me.txtLabel.Size = New System.Drawing.Size(270, 20)
        Me.txtLabel.TabIndex = 0
        Me.ToolTip1.SetToolTip(Me.txtLabel, "the name of the entity type. ")
        '
        'ToolTip1
        '
        Me.ToolTip1.IsBalloon = True
        Me.ToolTip1.ShowAlways = True
        Me.ToolTip1.ToolTipIcon = System.Windows.Forms.ToolTipIcon.Info
        '
        'txtDef
        '
        Me.txtDef.Location = New System.Drawing.Point(185, 29)
        Me.txtDef.Multiline = True
        Me.txtDef.Name = "txtDef"
        Me.txtDef.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtDef.Size = New System.Drawing.Size(270, 100)
        Me.txtDef.TabIndex = 1
        Me.ToolTip1.SetToolTip(Me.txtDef, "the description of the entity type. ")
        '
        'txtDefSource
        '
        Me.txtDefSource.Location = New System.Drawing.Point(185, 135)
        Me.txtDefSource.Multiline = True
        Me.txtDefSource.Name = "txtDefSource"
        Me.txtDefSource.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtDefSource.Size = New System.Drawing.Size(270, 100)
        Me.txtDefSource.TabIndex = 2
        Me.ToolTip1.SetToolTip(Me.txtDefSource, "the authority of the definition. ")
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label2.Location = New System.Drawing.Point(12, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(120, 23)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Entity Type Definition"
        Me.ToolTip1.SetToolTip(Me.Label2, "the description of the entity type. ")
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label3.Location = New System.Drawing.Point(12, 138)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(176, 23)
        Me.Label3.TabIndex = 5
        Me.Label3.Text = "Entity Type Definition Source"
        Me.ToolTip1.SetToolTip(Me.Label3, "the authority of the definition. ")
        '
        'btnOK
        '
        Me.btnOK.Enabled = False
        Me.btnOK.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnOK.Image = Global.MetaDoc.My.Resources.Resources.Ok_16x16
        Me.btnOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnOK.Location = New System.Drawing.Point(299, 241)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(75, 23)
        Me.btnOK.TabIndex = 3
        Me.btnOK.Text = "OK"
        Me.btnOK.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'btnCancel
        '
        Me.btnCancel.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.btnCancel.Image = Global.MetaDoc.My.Resources.Resources.Cancel_16x16
        Me.btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.btnCancel.Location = New System.Drawing.Point(380, 241)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(75, 23)
        Me.btnCancel.TabIndex = 4
        Me.btnCancel.Text = "Cancel"
        Me.btnCancel.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        '
        'combo_parentnode
        '
        Me.combo_parentnode.FormattingEnabled = True
        Me.combo_parentnode.Location = New System.Drawing.Point(12, 239)
        Me.combo_parentnode.Name = "combo_parentnode"
        Me.combo_parentnode.Size = New System.Drawing.Size(65, 21)
        Me.combo_parentnode.TabIndex = 15
        Me.combo_parentnode.Visible = False
        '
        'button_example
        '
        Me.button_example.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.button_example.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.button_example.Image = Global.MetaDoc.My.Resources.Resources.app_help_48x48
        Me.button_example.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.button_example.Location = New System.Drawing.Point(467, 12)
        Me.button_example.Name = "button_example"
        Me.button_example.Size = New System.Drawing.Size(110, 48)
        Me.button_example.TabIndex = 21
        Me.button_example.Text = "EXAMPLE"
        Me.button_example.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.button_example.UseVisualStyleBackColor = True
        '
        'frmEAdetail
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(589, 272)
        Me.ControlBox = False
        Me.Controls.Add(Me.button_example)
        Me.Controls.Add(Me.combo_parentnode)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.txtDefSource)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.txtDef)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.txtLabel)
        Me.Controls.Add(Me.Label1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.Name = "frmEAdetail"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Entity Type Description"
        Me.ToolTip1.SetToolTip(Me, "the definition and description of a set into which similar entity instances are c" & _
                "lassified. ")
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region

    Private Sub frmEAdetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'set up new form
        bUpdate = False
        txtLabel.Text = oEntity.EntityLabel
        txtDef.Text = oEntity.EntityDef
        txtDefSource.Text = oEntity.DefSource

        combo_parentnode.Items.Clear()

        For Each treenodeitem As TreeNode In frmTabDetailedDescription.treeNodes
            recursiveTreeView(treenodeitem)
        Next

        'Dim oNode As TreeNode = frmTabDetailedDescription.getTheSelectedNode()

        'If IsNothing(oNode) = False Then
        'combo_parentnode.SelectedIndex = combo_parentnode.FindString(oNode.Text)
        'Else
        combo_parentnode.SelectedIndex = combo_parentnode.FindString("ROOT")
        combo_parentnode.Enabled = False 'Entity can only be added to ROOT
        'End If
    End Sub

    Private Sub recursiveTreeView(ByVal n As TreeNode)
        combo_parentnode.Items.Add(n.Text)
        parentNodes.Add(n)
        For Each aNode As TreeNode In n.Nodes
            recursiveTreeView(aNode)
        Next
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If (txtLabel.Text = "") Then
            MessageBox.Show("The entity needs a label!", "Insufficient information in description", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If
        If (txtDef.Text = "") Then
            MessageBox.Show("The entity needs a definition!", "Insufficient information in description", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If
        If (txtDefSource.Text = "") Then
            MessageBox.Show("The entity needs a definition source!", "Insufficient information in description", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        'update the Entity; note that entity's attribute collection is not touched
        oEntity.EntityLabel = txtLabel.Text
        oEntity.EntityDef = txtDef.Text
        oEntity.DefSource = txtDefSource.Text
        oEntity.EntityType = frmTabDetailedDescription.getEntityType()

        bUpdate = True
        Dim thatNode As TreeNode = CType(parentNodes(combo_parentnode.SelectedIndex + 1), TreeNode)
        frmTabDetailedDescription.setSelectedNode2Add(combo_parentnode.SelectedItem.ToString(), thatNode)
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        bUpdate = False
        Me.Close()
    End Sub

    Private Sub txtLabel_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLabel.TextChanged
        checkRequiredFields()
    End Sub
    Private Sub checkRequiredFields()
        If txtDef.Text.Trim() = "" Or txtDefSource.Text.Trim() = "" Or txtLabel.Text.Trim() = "" Then btnOK.Enabled = False Else btnOK.Enabled = True
    End Sub

    Private Sub txtDef_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDef.TextChanged
        checkRequiredFields()
    End Sub

    Private Sub txtDefSource_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDefSource.TextChanged
        checkRequiredFields()
    End Sub

    Public Function getUpdated() As Boolean
        Return bUpdate
    End Function
    Public Function getEntity() As EAdetailClass
        Return oEntity
    End Function

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("EAdetail")
        helpForm.ShowDialog()
    End Sub
End Class
